package com.xxx.video.dynamic.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.AddFriendExtraMessageDialog;
import com.xxx.video.dynamic.beans.DynamicConfigBean;
import com.xxx.video.dynamic.beans.FriendListBean;
import com.xxx.video.main.beans.DynamicInfoBean;
import com.xxx.video.main.beans.LikeResultBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class DynamicDetailsPresenter extends BasePresenter<IDynamicDetailsView> {
    public DynamicDetailsPresenter(@NonNull Context context, IDynamicDetailsView baseView) {
        super(context, baseView);
    }

    /**
     * get dynamic info
     *
     * @param videoId id
     */
    public void getDynamicInfo(String videoId) {
        NetWorkUtils.getInstance().getDynamicInfo(videoId, new CommonResponseListener<DynamicInfoBean>() {

            @Override
            public void onSuccess(DynamicInfoBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initDynamicInfoData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(3);
                }
            }
        });
    }
    /**
     * comment
     *
     * @param videoId video id
     */
    public void interaction(String videoId) {
        String commentContent = getBaseView() == null ? "" : getBaseView().getComment();
        if (TextUtils.isEmpty(commentContent)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_comment_can_not_empty));
            return;
        }
        NetWorkUtils.getInstance().interaction(videoId, commentContent, new CommonResponseListener<BaseBean>() {

            @Override
            public void onSuccess(BaseBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().addCommentSuccess();
                        getBaseView().clearComment();
                        ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_comment_success));
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    /**
     * add browse
     *
     * @param videoId video id
     */
    public void browse(String videoId) {
        NetWorkUtils.getInstance().browse(videoId, String.valueOf(1),
                new CommonResponseListener<BaseBean>() {

                    @Override
                    public void onSuccess(BaseBean successOb, String result) {

                    }

                    @Override
                    public void onFailure(Object failureObj) {

                    }
                });

    }

    /**
     * star
     *
     * @param videoId video id
     */
    public void dynamicFabulous(String videoId) {
        if (!UserManager.getInstance().isLogin()) {
            if (getBaseView() != null) {
                getBaseView().startLoginActivity();
            }
            return;
        }
        NetWorkUtils.getInstance().dynamicFabulous(videoId, new CommonResponseListener<LikeResultBean>() {
            @Override
            public void onSuccess(LikeResultBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().starSuccess(successOb.praise);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    public void dynamicDislike(String fId) {
        if (!UserManager.getInstance().isLogin()) {
            if (getBaseView() != null) {
                getBaseView().startLoginActivity();
            }
            return;
        }
        NetWorkUtils.getInstance().dynamicDislike(fId, new CommonResponseListener<LikeResultBean>() {
            @Override
            public void onSuccess(LikeResultBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().unLikeSuccess(successOb.praise);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }
    /**
     * favorite
     *
     * @param status  status
     * @param videoId videoId
     */
    public void favorite(boolean status, String videoId) {
        if (!UserManager.getInstance().isLogin()) {
            if (getBaseView() != null) {
                getBaseView().startLoginActivity();
            }
            return;
        }
        if (!status) {
            NetWorkUtils.getInstance().collection(videoId, String.valueOf(1), new CommonResponseListener<BaseBean>() {
                @Override
                public void onSuccess(BaseBean successOb, String result) {
                    if (ResponseUtils.getInstance().isNoError(successOb)) {
                        if (getBaseView() != null) {
                            getBaseView().collectionSuccess();
                        }
                    }
                }

                @Override
                public void onFailure(Object failureObj) {

                }
            });
        }else{
            if (getBaseView() != null) {
                ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_collection_yet));
                getBaseView().startFavoriteActivity();
            }
        }
    }
    /**
     * add play
     *
     * @param videoId video id
     */
    public void play(String videoId,long startTime) {
        NetWorkUtils.getInstance().play(videoId, String.valueOf(1),startTime,
                new CommonResponseListener<BaseBean>() {

                    @Override
                    public void onSuccess(BaseBean successOb, String result) {

                    }

                    @Override
                    public void onFailure(Object failureObj) {

                    }
                });
    }

    public void addFriends(DynamicInfoBean.DynamicBean dynamicBean) {
        AddFriendExtraMessageDialog addFriendExtraMessageDialog = new AddFriendExtraMessageDialog(context);
        addFriendExtraMessageDialog.show();
        addFriendExtraMessageDialog.setOnButtonClickListener(message -> NetWorkUtils.getInstance().addFriends(dynamicBean.fUserId, dynamicBean.fUserName, "", dynamicBean.fUsePicurl, message, new CommonResponseListener<BaseBean>() {

            @Override
            public void onSuccess(BaseBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().addFriendSuccess();
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        }));
    }

    public void getDynamicReview() {
        NetWorkUtils.getInstance().getDynamicReview(new CommonResponseListener<DynamicConfigBean>() {

            @Override
            public void onSuccess(DynamicConfigBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initPreview(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }
}
